package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.DistributionInstallDao;
import com.shop.cloud.domain.hongfu.DistributionInstall;
import com.shop.cloud.service.hongfu.IDistributionInstallService;

/**
 * 
 */
@Transactional
@Service("distributionInstallService")
public class DistributionInstallService extends BaseService<DistributionInstall> implements IDistributionInstallService{
    
    @Autowired
	private DistributionInstallDao distributionInstallDao;

	@Override
	public MybatisDao<DistributionInstall> getEntityDao() {
		return distributionInstallDao;
	}

	/**
	 * 添加分销设置信息
	 */
	@Override
	public boolean handleInsertDistributionInstall(String distriSetting, int type, Long id) {
		boolean resultFlag = false;
		
		if(StringUtils.isNotEmpty(distriSetting)){
			
			DistributionInstall distributionInstall = new DistributionInstall();
			distributionInstall.setDistriSetting(distriSetting);
			distributionInstall.setFromType(type);
			distributionInstall.setFromId(id);
			distributionInstall.setCreateTime(new Timestamp((new Date()).getTime()));
			if(type == 0){
				distributionInstall.setRemark("商品分销设置");
			}
			if(type == 1){
				distributionInstall.setRemark("活动分销设置");
			}
			
			resultFlag = this.insert(distributionInstall);
		}
		
		return resultFlag;
	}

	/**
	 * 修改分销设置信息
	 */
	@Override
	public int handleModifyDistributionInstall(String distriSetting, int type, Long id) {
		int resultInt = 0;
		
		if(StringUtils.isNotEmpty(distriSetting)){
			DistributionInstall distributionInstall = new DistributionInstall();
			if(type == 0){ //商品
				distributionInstall = distributionInstallDao.getByTypeAndID(id);
			}
			if(type == 1){ //活动
				distributionInstall = distributionInstallDao.getByTypeAndUser(id);
			}
			
			if(null != distributionInstall){
				distributionInstall.setDistriSetting(distriSetting);
				//更新
				resultInt = this.update(distributionInstall);
			}
		}
		
		return resultInt;
	}
    
}